package com.fu.edu.manager.service;

import com.fu.edu.manager.dao.BaseStudentDao;
import com.fu.edu.manager.dao.OtherStudentDao;
import com.fu.edu.manager.dao.StudentDao;
import com.fu.edu.manager.domain.Student;
import com.fu.edu.manager.factory.StudentDaoFactory;

/**
 * @author 付大大
 * @create 2021-01-06 10:54
 */
public class StudentService {

    //    private OtherStudentDao studentDao = new OtherStudentDao();
    //通过学生库管工厂类，获取库管对象
    private BaseStudentDao studentDao = StudentDaoFactory.getStudentDao();

    public boolean addStudent(Student student) {
        return studentDao.addStudent(student);
    }

    public boolean isExists(String id) {

        Student[] students = studentDao.findAllStudent();
        //假设id在数组中不存在
        boolean exists = false;
        //遍历数组,获取每一个学生对象，准备进行判断
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    public Student[] findAllStudent() {
        Student[] allStudent = studentDao.findAllStudent();
        boolean flag = false;
        for (int i = 0; i < allStudent.length; i++) {
            Student student = allStudent[i];
            if (student != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return allStudent;
        } else {
            return null;
        }
    }

    public void deleteById(String id) {
        studentDao.deleteStudentById(id);
    }

    public void updateStudent(Student student, String id) {
        studentDao.updateStudent(student, id);
    }
}
